import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { HTTP_BASE_URL, SERVERS_NAME } from '../../../config/config';
import {ApprovalFormService} from './approval-form.service';
import {StatusEnum} from '../../../core/enum/status.enum';
import {NzMessageService} from 'ng-zorro-antd';

@Component({
  selector: 'app-approval-form',
  templateUrl: './approval-form.component.html',
  styleUrls: ['./approval-form.component.scss'],
  providers: [ApprovalFormService]
})
export class ApprovalFormComponent implements OnInit {
  @Input() currObj: any;
  @Input() currType: number; // 0,驳回，1，通过，2，详情
  @Input() pageType: number; // 1,制度审批，2，反馈审批
  @Output() backemit = new EventEmitter();

  imgPixe = `${HTTP_BASE_URL}${SERVERS_NAME.SECURITY_SERVER}source/view/`;
  materialObj = null;
  remark = null;

  stateObj = {
    0: '新增',
    5: '修改',
    10: '停用'
  };

  constructor(
    private service: ApprovalFormService,
    private message: NzMessageService,
  ) { }

  ngOnInit() {
    this.getDetails();
  }

  // tabs数据查询
  getDetails() {
    let opt = null;
    let par1 = null;
    if (this.pageType == 1) {
      opt = 'view';
      par1 = this.currObj.id;
    } else if (this.pageType == 2) {
      opt = 'view1';
      par1 = {
        id: this.currObj.id
      };
    }
    this.service[opt](par1).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.materialObj = res.datas;
        if (this.materialObj.sourceList && this.materialObj.sourceList.length > 0) {
          this.materialObj.source = this.imgPixe + this.materialObj.sourceList[0].moduleName + '/' + this.materialObj.sourceList[0].fileNewName;
        }
      }
    });
  }

  /**
   * pdf页面载入时,跳转到最新的那一页
   */
  pdfLoad(e) {
  }

  approvalChange(e) {
    this.currType = e;
    if (this.currType == 1) {
      this.remark = null;
    }
  }

  back(flag) {
    this.backemit.emit(flag);
  }

  // 审核
  auditClick() {
    const obj = {
      id: this.currObj.id,
      isPass: this.currType
    };
    if (this.currType == 0) {
      if (!this.remark) {
        this.message.warning('审批驳回时，理由不能为空!');
        return;
      } else {
        obj['reason'.toString()] = this.remark;
      }
    } else {
      obj['reason'.toString()] = null;
    }
    let opt = null;
    if (this.pageType == 1) { // 制度审批
      opt = 'checks';
    } else if (this.pageType == 2) { // 反馈审批
      opt = 'checks1';
    }
    this.service[opt](obj).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.message.success(res.message);
        this.back(true);
      }
    });
  }

}
